{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "93b6daa6",
   "metadata": {},
   "source": [
    "## 1. APP 币本位合约交易-市价单开仓函数总览"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9f6c197e",
   "metadata": {},
   "source": [
    "|方法|解释|\n",
    "|:---|:---|\n",
    "|open_market|市价单开仓|"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "346f1efa",
   "metadata": {},
   "source": [
    "## 2. 模型实例化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "763abbe8",
   "metadata": {},
   "outputs": [],
   "source": [
    "from binance_interface.app import BinanceCM\n",
    "from pprint import pprint"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f3b40e87",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 转发：需搭建转发服务器，可参考：https://github.com/pyted/binance_resender\n",
    "proxy_host = None\n",
    "key = 'xxxx'\n",
    "secret = 'xxxx'\n",
    "\n",
    "binanceCM = BinanceCM(\n",
    "    key=key, secret=secret,\n",
    "    proxy_host=proxy_host\n",
    ")\n",
    "trade = binanceCM.trade"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fa7dbce2",
   "metadata": {},
   "source": [
    "## 3. 同步 市价开仓"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "eda09f50",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'cancel_result': None,\n",
      " 'error_result': None,\n",
      " 'func_param': {'callback': None,\n",
      "                'cancel': True,\n",
      "                'delay': 0.2,\n",
      "                'errorback': None,\n",
      "                'leverage': 1,\n",
      "                'marginType': 'ISOLATED',\n",
      "                'meta': {},\n",
      "                'newClientOrderId': '',\n",
      "                'newThread': False,\n",
      "                'openMoney': 0.01,\n",
      "                'positionSide': 'LONG',\n",
      "                'quantity': None,\n",
      "                'symbol': 'ETHUSD_PERP',\n",
      "                'timeout': 60},\n",
      " 'get_order_result': {'code': 200,\n",
      "                      'data': {'avgPrice': '2191.19',\n",
      "                               'clientOrderId': 'PY26WTMO0DlARJIK0fV5y2',\n",
      "                               'closePosition': False,\n",
      "                               'cumBase': '0.00912746',\n",
      "                               'executedQty': '2',\n",
      "                               'orderId': 70558910259,\n",
      "                               'origQty': '2',\n",
      "                               'origType': 'MARKET',\n",
      "                               'pair': 'ETHUSD',\n",
      "                               'positionSide': 'LONG',\n",
      "                               'price': '0',\n",
      "                               'priceProtect': False,\n",
      "                               'reduceOnly': False,\n",
      "                               'side': 'BUY',\n",
      "                               'status': 'FILLED',\n",
      "                               'stopPrice': '0',\n",
      "                               'symbol': 'ETHUSD_PERP',\n",
      "                               'time': 1706200083431,\n",
      "                               'timeInForce': 'GTC',\n",
      "                               'type': 'MARKET',\n",
      "                               'updateTime': 1706200083431,\n",
      "                               'workingType': 'CONTRACT_PRICE'},\n",
      "                      'msg': ''},\n",
      " 'meta': {},\n",
      " 'request_param': {'newClientOrderId': '',\n",
      "                   'positionSide': 'LONG',\n",
      "                   'quantity': '2',\n",
      "                   'side': 'BUY',\n",
      "                   'symbol': 'ETHUSD_PERP',\n",
      "                   'type': 'MARKET'},\n",
      " 'set_order_result': {'code': 200,\n",
      "                      'data': {'avgPrice': '0.00',\n",
      "                               'clientOrderId': 'PY26WTMO0DlARJIK0fV5y2',\n",
      "                               'closePosition': False,\n",
      "                               'cumBase': '0',\n",
      "                               'cumQty': '0',\n",
      "                               'executedQty': '0',\n",
      "                               'orderId': 70558910259,\n",
      "                               'origQty': '2',\n",
      "                               'origType': 'MARKET',\n",
      "                               'pair': 'ETHUSD',\n",
      "                               'positionSide': 'LONG',\n",
      "                               'price': '0',\n",
      "                               'priceProtect': False,\n",
      "                               'reduceOnly': False,\n",
      "                               'side': 'BUY',\n",
      "                               'status': 'NEW',\n",
      "                               'stopPrice': '0',\n",
      "                               'symbol': 'ETHUSD_PERP',\n",
      "                               'timeInForce': 'GTC',\n",
      "                               'type': 'MARKET',\n",
      "                               'updateTime': 1706200083431,\n",
      "                               'workingType': 'CONTRACT_PRICE'},\n",
      "                      'msg': ''},\n",
      " 'status': 'FILLED',\n",
      " 'symbol': 'ETHUSD_PERP'}\n"
     ]
    }
   ],
   "source": [
    "open_market3 = trade.open_market(\n",
    "    symbol='ETHUSD_PERP',  # 产品\n",
    "    marginType='ISOLATED',  # 保证金模式 ISOLATED: 逐仓 CROSSED: 全仓\n",
    "    positionSide='LONG',  # 持仓方向 LONG: 多单 SHORT: 空单\n",
    "    leverage=1,  # 杠杆\n",
    "    openMoney=0.01,  # 开仓金额(货币数量) 开仓金额openMoney和开仓数量quantity必须输入其中一个 优先级：quantity > openMoney\n",
    "    # quantity=1,  # 开仓数量（合约张数）\n",
    "    newClientOrderId='',  # 客户自定义订单ID\n",
    ")\n",
    "\n",
    "pprint(open_market3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e122fe90",
   "metadata": {},
   "source": [
    "## 4. 同步 市价开仓 回调函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "001d0d8e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 执行成功回调\n",
    "def callback4(information):\n",
    "    print('callback')\n",
    "    pprint(information)\n",
    "\n",
    "\n",
    "# 执行错误回调\n",
    "def errorback4(information):\n",
    "    print('errorback')\n",
    "    pprint(information)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "f5dc3672",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "callback\n",
      "{'cancel_result': None,\n",
      " 'error_result': None,\n",
      " 'func_param': {'callback': <function callback4 at 0x7fc8988d3b80>,\n",
      "                'cancel': True,\n",
      "                'delay': 0.2,\n",
      "                'errorback': <function errorback4 at 0x7fc8988d3790>,\n",
      "                'leverage': 1,\n",
      "                'marginType': 'ISOLATED',\n",
      "                'meta': {},\n",
      "                'newClientOrderId': '',\n",
      "                'newThread': False,\n",
      "                'openMoney': 0.01,\n",
      "                'positionSide': 'LONG',\n",
      "                'quantity': None,\n",
      "                'symbol': 'ETHUSD_PERP',\n",
      "                'timeout': 60},\n",
      " 'get_order_result': {'code': 200,\n",
      "                      'data': {'avgPrice': '2190.78',\n",
      "                               'clientOrderId': 'WzQxgQCEEyIQn5agfAJJzz',\n",
      "                               'closePosition': False,\n",
      "                               'cumBase': '0.00912916',\n",
      "                               'executedQty': '2',\n",
      "                               'orderId': 70558966251,\n",
      "                               'origQty': '2',\n",
      "                               'origType': 'MARKET',\n",
      "                               'pair': 'ETHUSD',\n",
      "                               'positionSide': 'LONG',\n",
      "                               'price': '0',\n",
      "                               'priceProtect': False,\n",
      "                               'reduceOnly': False,\n",
      "                               'side': 'BUY',\n",
      "                               'status': 'FILLED',\n",
      "                               'stopPrice': '0',\n",
      "                               'symbol': 'ETHUSD_PERP',\n",
      "                               'time': 1706200114709,\n",
      "                               'timeInForce': 'GTC',\n",
      "                               'type': 'MARKET',\n",
      "                               'updateTime': 1706200114709,\n",
      "                               'workingType': 'CONTRACT_PRICE'},\n",
      "                      'msg': ''},\n",
      " 'meta': {},\n",
      " 'request_param': {'newClientOrderId': '',\n",
      "                   'positionSide': 'LONG',\n",
      "                   'quantity': '2',\n",
      "                   'side': 'BUY',\n",
      "                   'symbol': 'ETHUSD_PERP',\n",
      "                   'type': 'MARKET'},\n",
      " 'set_order_result': {'code': 200,\n",
      "                      'data': {'avgPrice': '0.00',\n",
      "                               'clientOrderId': 'WzQxgQCEEyIQn5agfAJJzz',\n",
      "                               'closePosition': False,\n",
      "                               'cumBase': '0',\n",
      "                               'cumQty': '0',\n",
      "                               'executedQty': '0',\n",
      "                               'orderId': 70558966251,\n",
      "                               'origQty': '2',\n",
      "                               'origType': 'MARKET',\n",
      "                               'pair': 'ETHUSD',\n",
      "                               'positionSide': 'LONG',\n",
      "                               'price': '0',\n",
      "                               'priceProtect': False,\n",
      "                               'reduceOnly': False,\n",
      "                               'side': 'BUY',\n",
      "                               'status': 'NEW',\n",
      "                               'stopPrice': '0',\n",
      "                               'symbol': 'ETHUSD_PERP',\n",
      "                               'timeInForce': 'GTC',\n",
      "                               'type': 'MARKET',\n",
      "                               'updateTime': 1706200114709,\n",
      "                               'workingType': 'CONTRACT_PRICE'},\n",
      "                      'msg': ''},\n",
      " 'status': 'FILLED',\n",
      " 'symbol': 'ETHUSD_PERP'}\n"
     ]
    }
   ],
   "source": [
    "# 设置callback与errorback\n",
    "open_market4 = trade.open_market(\n",
    "    symbol='ETHUSD_PERP',  # 产品\n",
    "    marginType='ISOLATED',  # 保证金模式 ISOLATED: 逐仓 CROSSED: 全仓\n",
    "    positionSide='LONG',  # 持仓方向 LONG: 多单 SHORT: 空单\n",
    "    leverage=1,  # 杠杆\n",
    "    openMoney=0.01,  # 开仓金额(货币数量) 开仓金额openMoney和开仓数量quantity必须输入其中一个 优先级：quantity > openMoney\n",
    "    # quantity=1,  # 开仓数量（合约张数）\n",
    "    newClientOrderId='',  # 客户自定义订单ID\n",
    "    meta={},  # 向回调函数中传递的参数字典\n",
    "\n",
    "    callback=callback4,  # 开仓成功触发的回调函数\n",
    "    errorback=errorback4,  # 开仓失败触发的回调函数\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "abd95726",
   "metadata": {},
   "source": [
    "## 5. 异步 市价开仓（购买）回调函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "815ff491",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 执行成功回调\n",
    "def callback5(information):\n",
    "    print('thread callback')\n",
    "    pprint(information)\n",
    "\n",
    "\n",
    "# 执行错误回调\n",
    "def errorback5(information):\n",
    "    print('thread errorback')\n",
    "    pprint(information)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "b687dcea",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<Thread(Thread-5, started 123145698799616)>\n",
      "------------------------------\n",
      "thread callback\n",
      "{'cancel_result': None,\n",
      " 'error_result': None,\n",
      " 'func_param': {'callback': <function callback5 at 0x7ff9307379d0>,\n",
      "                'cancel': True,\n",
      "                'delay': 0.2,\n",
      "                'errorback': <function errorback5 at 0x7ff961825ca0>,\n",
      "                'leverage': 1,\n",
      "                'marginType': 'ISOLATED',\n",
      "                'meta': {},\n",
      "                'newClientOrderId': '',\n",
      "                'newThread': True,\n",
      "                'openMoney': 0.01,\n",
      "                'positionSide': 'LONG',\n",
      "                'quantity': None,\n",
      "                'symbol': 'ETHUSD_PERP',\n",
      "                'timeout': 60},\n",
      " 'get_order_result': {'code': 200,\n",
      "                      'data': {'avgPrice': '2191.38',\n",
      "                               'clientOrderId': 'BJShTZRtigQoTiFFKPeasy',\n",
      "                               'closePosition': False,\n",
      "                               'cumBase': '0.00912666',\n",
      "                               'executedQty': '2',\n",
      "                               'orderId': 70559081963,\n",
      "                               'origQty': '2',\n",
      "                               'origType': 'MARKET',\n",
      "                               'pair': 'ETHUSD',\n",
      "                               'positionSide': 'LONG',\n",
      "                               'price': '0',\n",
      "                               'priceProtect': False,\n",
      "                               'reduceOnly': False,\n",
      "                               'side': 'BUY',\n",
      "                               'status': 'FILLED',\n",
      "                               'stopPrice': '0',\n",
      "                               'symbol': 'ETHUSD_PERP',\n",
      "                               'time': 1706200213011,\n",
      "                               'timeInForce': 'GTC',\n",
      "                               'type': 'MARKET',\n",
      "                               'updateTime': 1706200213011,\n",
      "                               'workingType': 'CONTRACT_PRICE'},\n",
      "                      'msg': ''},\n",
      " 'meta': {},\n",
      " 'request_param': {'newClientOrderId': '',\n",
      "                   'positionSide': 'LONG',\n",
      "                   'quantity': '2',\n",
      "                   'side': 'BUY',\n",
      "                   'symbol': 'ETHUSD_PERP',\n",
      "                   'type': 'MARKET'},\n",
      " 'set_order_result': {'code': 200,\n",
      "                      'data': {'avgPrice': '0.00',\n",
      "                               'clientOrderId': 'BJShTZRtigQoTiFFKPeasy',\n",
      "                               'closePosition': False,\n",
      "                               'cumBase': '0',\n",
      "                               'cumQty': '0',\n",
      "                               'executedQty': '0',\n",
      "                               'orderId': 70559081963,\n",
      "                               'origQty': '2',\n",
      "                               'origType': 'MARKET',\n",
      "                               'pair': 'ETHUSD',\n",
      "                               'positionSide': 'LONG',\n",
      "                               'price': '0',\n",
      "                               'priceProtect': False,\n",
      "                               'reduceOnly': False,\n",
      "                               'side': 'BUY',\n",
      "                               'status': 'NEW',\n",
      "                               'stopPrice': '0',\n",
      "                               'symbol': 'ETHUSD_PERP',\n",
      "                               'timeInForce': 'GTC',\n",
      "                               'type': 'MARKET',\n",
      "                               'updateTime': 1706200213011,\n",
      "                               'workingType': 'CONTRACT_PRICE'},\n",
      "                      'msg': ''},\n",
      " 'status': 'FILLED',\n",
      " 'symbol': 'ETHUSD_PERP'}\n"
     ]
    }
   ],
   "source": [
    "# 设置newThread=True\n",
    "open_market5 = trade.open_market(\n",
    "    symbol='ETHUSD_PERP',  # 产品\n",
    "    marginType='ISOLATED',  # 保证金模式 ISOLATED: 逐仓 CROSSED: 全仓\n",
    "    positionSide='LONG',  # 持仓方向 LONG: 多单 SHORT: 空单\n",
    "    leverage=1,  # 杠杆\n",
    "    openMoney=0.01,  # 开仓金额(货币数量) 开仓金额openMoney和开仓数量quantity必须输入其中一个 优先级：quantity > openMoney\n",
    "    # quantity=1,  # 开仓数量（合约张数）\n",
    "    newClientOrderId='',  # 客户自定义订单ID\n",
    "    callback=callback5,  # 开仓成功触发的回调函数\n",
    "    errorback=errorback5,  # 开仓失败触发的回调函数\n",
    "\n",
    "    newThread=True,  # 是否开启一个新的线程维护这个订单\n",
    ")\n",
    "\n",
    "print(open_market5)\n",
    "print('-' * 30)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
